package com.uniflow.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.time.LocalDateTime;
import java.util.List;

/**
 * API密钥实体类
 * 
 * @author uniflow
 * @since 1.0.0
 */
@ApiModel(description = "API密钥")
@TableName("api_keys")
public class ApiKey {
    
    @ApiModelProperty("密钥ID")
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private String id;
    
    @ApiModelProperty("应用ID")
    @TableField("app_id")
    private String appId;
    
    @ApiModelProperty("访问密钥")
    @TableField("access_key")
    private String accessKey;
    
    @ApiModelProperty("秘密密钥")
    @TableField("secret_key")
    private String secretKey;
    
    @ApiModelProperty("API权限列表")
    @TableField("permissions")
    private String permissions;
    
    @ApiModelProperty("过期时间")
    @TableField("expires_at")
    private LocalDateTime expiresAt;
    
    @ApiModelProperty("创建时间")
    @TableField("created_at")
    private LocalDateTime createdAt;
    
    // 非数据库字段
    @ApiModelProperty("应用信息")
    @TableField(exist = false)
    private ApiApplication application;
    
    @ApiModelProperty("权限列表")
    @TableField(exist = false)
    private List<String> permissionList;
    
    @ApiModelProperty("是否过期")
    @TableField(exist = false)
    private Boolean expired;
    
    @ApiModelProperty("剩余有效天数")
    @TableField(exist = false)
    private Long remainingDays;
    
    // 构造函数
    public ApiKey() {
        this.createdAt = LocalDateTime.now();
    }
    
    public ApiKey(String appId, String accessKey, String secretKey) {
        this();
        this.appId = appId;
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }
    
    // Getter和Setter方法
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getAppId() {
        return appId;
    }
    
    public void setAppId(String appId) {
        this.appId = appId;
    }
    
    public String getAccessKey() {
        return accessKey;
    }
    
    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }
    
    public String getSecretKey() {
        return secretKey;
    }
    
    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }
    
    public String getPermissions() {
        return permissions;
    }
    
    public void setPermissions(String permissions) {
        this.permissions = permissions;
    }
    
    public LocalDateTime getExpiresAt() {
        return expiresAt;
    }
    
    public void setExpiresAt(LocalDateTime expiresAt) {
        this.expiresAt = expiresAt;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public ApiApplication getApplication() {
        return application;
    }
    
    public void setApplication(ApiApplication application) {
        this.application = application;
    }
    
    public List<String> getPermissionList() {
        return permissionList;
    }
    
    public void setPermissionList(List<String> permissionList) {
        this.permissionList = permissionList;
    }
    
    public Boolean getExpired() {
        return expired;
    }
    
    public void setExpired(Boolean expired) {
        this.expired = expired;
    }
    
    public Long getRemainingDays() {
        return remainingDays;
    }
    
    public void setRemainingDays(Long remainingDays) {
        this.remainingDays = remainingDays;
    }
    
    // 业务方法
    
    /**
     * 判断是否过期
     */
    public boolean isExpired() {
        if (expiresAt == null) {
            return false; // 永不过期
        }
        return LocalDateTime.now().isAfter(expiresAt);
    }
    
    /**
     * 检查是否有指定权限
     */
    public boolean hasPermission(String permission) {
        if (permissions == null || permissions.trim().isEmpty()) {
            return false;
        }
        
        // 支持通配符权限
        if (permissions.contains("*")) {
            return true;
        }
        
        String[] perms = permissions.split(",");
        for (String perm : perms) {
            if (perm.trim().equals(permission)) {
                return true;
            }
            // 支持模式匹配，如 "user:*" 匹配 "user:read"
            if (perm.trim().endsWith(":*") && permission.startsWith(perm.trim().replace(":*", ":"))) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * 获取剩余有效天数
     */
    public long getRemainingDaysCount() {
        if (expiresAt == null) {
            return Long.MAX_VALUE; // 永不过期
        }
        
        LocalDateTime now = LocalDateTime.now();
        if (now.isAfter(expiresAt)) {
            return 0; // 已过期
        }
        
        return java.time.Duration.between(now, expiresAt).toDays();
    }
    
    /**
     * 获取状态描述
     */
    public String getStatusDesc() {
        if (isExpired()) {
            return "已过期";
        }
        
        if (expiresAt == null) {
            return "永久有效";
        }
        
        long days = getRemainingDaysCount();
        if (days <= 7) {
            return "即将过期 (" + days + "天)";
        }
        
        return "有效";
    }
    
    /**
     * 掩码显示密钥
     */
    public String getMaskedSecretKey() {
        if (secretKey == null || secretKey.length() <= 8) {
            return "****";
        }
        
        return secretKey.substring(0, 4) + "****" + secretKey.substring(secretKey.length() - 4);
    }
    
    @Override
    public String toString() {
        return "ApiKey{" +
                "id='" + id + '\'' +
                ", appId='" + appId + '\'' +
                ", accessKey='" + accessKey + '\'' +
                ", permissions='" + permissions + '\'' +
                ", expiresAt=" + expiresAt +
                ", createdAt=" + createdAt +
                '}';
    }
}